package com.zenithsun.common.util.image;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.coobird.thumbnailator.Thumbnails;

public class Test {

	public static void main(String... args) throws Exception {
		//CreateJson("E:\\南网\\南网传媒\\02设计文档\\效果设计\\素材原型\\云南电网\\全面创先");
		//CreateJson("D:\\bak\\");
	}

	public static void CreateJson(String filePath) throws Exception {
		File file = new File(filePath);
		List<JsonNews> list = new ArrayList<JsonNews>();
		int i = 0;
		for (File fileItem : file.listFiles()) {
			if(!fileItem.isFile()||fileItem.getName().lastIndexOf(".tif")>0){
				continue;
			}
			/*
			JsonNews newsInfo = new JsonNews();
			newsInfo.setBrowseCount((int) (Math.random() * 10000));
			newsInfo.setPic(i
					+ fileItem.getName().substring(
							fileItem.getName().lastIndexOf(".")));
			newsInfo.setPhotographer(getPhotographer(fileItem.getName()));
			newsInfo.setContent(fileItem.getName().substring(0,fileItem.getName().lastIndexOf(".") - 1));
			newsInfo.setCreateDate(getDate(fileItem.getName()));
			if (newsInfo.getPhotographer() != null) {
				newsInfo.setContent(newsInfo.getContent().replace(newsInfo.getPhotographer(), ""));
			}
			newsInfo.setPhotographer(getPhotographerReplace(newsInfo.getPhotographer()));
			if (newsInfo.getCreateDate() == null) {
			}
			if (newsInfo.getEditor() == null) {
				newsInfo.setEditor(newsInfo.getPhotographer());
			}
			list.add(newsInfo);
			*/
			copyFile(fileItem,new File("d:\\test\\"+i+fileItem.getName().substring(fileItem.getName().lastIndexOf("."))));
			i++;
		}
	}

	private static void copyFile(File source, File target) {
		try {
			//Thumbnails.of(source).width(750).toFile(target);
			Thumbnails.of(source).sourceRegion(100, 100, 600, 600).size(300, 300).keepAspectRatio(false).toFile(target);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		/*
		 * FileInputStream input = null; FileOutputStream output = null; try {
		 * input = new FileInputStream(source); output = new
		 * FileOutputStream(target); int in = input.read(); while (in != -1) {
		 * output.write(in); in = input.read(); } } catch (IOException e) {
		 * } finally { try { if (input !=
		 * null) { input.close(); } if (output != null) { output.flush(); } }
		 * catch (Exception e) { } }
		 */
	}

	/**
	 * 验证时间字符串格式输入是否正确
	 * 
	 * @param timeStr
	 * @return
	 */
	private static String getPhotographer(String str) {
		String[] regs = { "。[\\w\\W]+摄","。[\\w\\W]+图","。[\\w\\W]+拍", "（[\\w\\W]+摄）", "([\\w\\W]+摄)" ,"（[\\w\\W]+）"," [\\w\\W]+摄"}; 
		for (String reg : regs) {
			Pattern pattern = Pattern.compile(reg);
			Matcher matcher = pattern.matcher(str);
			while (matcher.find()) {
				return matcher.group();
			}
		}
		return null;
	}

	private static String getPhotographerReplace(String find) {
		if (find != null) {
			find = find.replace("。", "");
			find = find.replace("（", "");
			find = find.replace("）", "");
			find = find.replace("( ", "");
			find = find.replace(") ", "");
			find = find.replace("摄", "");
			return find.trim();
		}
		return null;
	}

	/**
	 * 验证时间字符串格式输入是否正确
	 * 
	 * @param timeStr
	 * @return
	 */
	public static String getDate(String str) {
		String reg = "(\\d{4}年\\d{1,2}月\\d{1,2}日)";
		Pattern pattern = Pattern.compile(reg);
		Matcher matcher = pattern.matcher(str);
		while (matcher.find()) {
			return StringPattern(matcher.group());
		}
		return null;
	}

	/**
	 * 将String型格式化,比如想要将2011-11-11格式化成2011年11月11日,就StringPattern("2011-11-11",
	 * "yyyy-MM-dd","yyyy年MM月dd日").
	 * 
	 * @param date
	 *            String 想要格式化的日期
	 * @param oldPattern
	 *            String 想要格式化的日期的现有格式
	 * @param newPattern
	 *            String 想要格式化成什么格式
	 * @return String
	 */
	private static String StringPattern(String date) {
		if (date == null){
			return "";
		}
		String oldPattern="yyyy年MM月dd日";
		String newPattern="yyyy-MM-dd";
		SimpleDateFormat sdf1 = new SimpleDateFormat(oldPattern); // 实例化模板对象
		SimpleDateFormat sdf2 = new SimpleDateFormat(newPattern); // 实例化模板对象
		Date d = null;
		try {
			d = sdf1.parse(date); // 将给定的字符串中的日期提取出来
		} catch (Exception e) { // 如果提供的字符串格式有错误，则进行异常处理
			e.printStackTrace(); // 打印异常信息
		}
		return sdf2.format(d);
	}
}